var Goals = Class.create();

Object.extend(Goals, {
  PENDING: 0,
  SUCCESS: 1,
  FAILURE: 2,

  selectNoInterval: function() {
    if ($('goal_no_interval').checked) {
      $('goal_interval').value = '';
      $('goal_interval').disable();
      $('goal_interval_unit').disable();
    }
    else {
      $('goal_interval').enable();
      $('goal_interval_unit').enable();
    }
  },
  
  changeState: function(checkpoint_id, state) {
    if (state == Goals.PENDING) {
      state_color = 'pending';
      state_text  = 'Pending';
    }
    else if (state == Goals.SUCCESS) {
      state_color = 'success';
      state_text  = 'Success';
    }
    else if (state == Goals.FAILURE) {
      state_color = 'failure';
      state_text  = 'Failure';
    }
  
    new Ajax.Request(
      '/admin/goal_checkpoint_update/' + checkpoint_id + '/' + state,
      { asynchronous: true,
        evalScripts: true,
        onSuccess: function(request) {
          el = $('goal_checkpoint_' + checkpoint_id);
          el_state_color = el.down('td.state_color');
          el_state_color.removeClassName('pending');
          el_state_color.removeClassName('success');
          el_state_color.removeClassName('failure');
          el_state_color.addClassName(state_color);
          el_state_text = el.down('td.state_text');
          el_state_text.update(state_text);
        },
        onFailure: function(request) {
          alert('uh oh');
        } } );
  }
});

